package com.bank.ui.admin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import com.bank.dao.DepositTypeDAO;
import com.bank.entity.DepositType;

public class AddDepositTypeDialog extends JDialog {
    private JTextField savingIDField;
    private JTextField savingNameField;
    private JTextArea descriptArea;

    public AddDepositTypeDialog() {
        setTitle("添加存款业务");
        setSize(400, 300);
        setModal(true);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout(10, 10));

        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(8, 8, 8, 8);
        gbc.anchor = GridBagConstraints.WEST;

        // 业务类型编号
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(new JLabel("业务类型编号:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        savingIDField = new JTextField(15);
        mainPanel.add(savingIDField, gbc);

        // 业务名称
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(new JLabel("业务名称:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        savingNameField = new JTextField(15);
        mainPanel.add(savingNameField, gbc);

        // 描述
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(new JLabel("描述:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.BOTH;
        descriptArea = new JTextArea(4, 20);
        descriptArea.setLineWrap(true);
        descriptArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(descriptArea);
        mainPanel.add(scrollPane, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(this::saveDepositType);

        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void saveDepositType(ActionEvent e) {
        String savingIDStr = savingIDField.getText().trim();
        String savingName = savingNameField.getText().trim();
        String descript = descriptArea.getText().trim();

        if (savingIDStr.isEmpty() || savingName.isEmpty()) {
            JOptionPane.showMessageDialog(this,
                    "业务类型编号和业务名称不能为空",
                    "输入错误",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            int savingID = Integer.parseInt(savingIDStr);
            DepositType depositType = new DepositType(savingID, savingName, descript);
            DepositTypeDAO depositTypeDAO = new DepositTypeDAO();
            depositTypeDAO.addDepositType(depositType);

            JOptionPane.showMessageDialog(this,
                    "存款业务添加成功",
                    "成功",
                    JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this,
                    "业务类型编号必须是数字",
                    "输入错误",
                    JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    "存款业务添加失败: " + ex.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}